package arithmetic.LeetCode;

import java.util.Arrays;

/**
 * @author jiangfeng on 2023/6/7
 */
public class LargestNumber {
    static class Solution {
        // 直接排序, 比较两个数拼接在一起是前后大
        public String largestNumber(int[] nums) {
            if (nums.length == 0) {
                return "";
            }
            String[] numStr = new String[nums.length];
            for (int i = 0; i < nums.length; i++) {
                numStr[i] = String.valueOf(nums[i]);
            }
            Arrays.sort(numStr, (x, y) -> {
                return (y + x).compareTo(x + y);
            });
            if ("0".equals(numStr[0])) {
                return "0";
            }
            StringBuilder sb = new StringBuilder();
            for (String n : numStr) {
                sb.append(n);
            }
            return sb.toString();
        }
    }
}
